package com.link.base.base.coupons.controller;

import com.link.base.core.basic.util.YmlConstants;
import com.link.base.user.model.CoreUser;
import com.link.base.base.coupons.model.Coupons;
import com.link.base.base.coupons.service.CouponsService;
import com.link.base.base.email.model.EmailInfo;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>优惠券活动生成二维码Controller</p>
 * 客户扫码跳转优惠券活动
 * @author wangdawei
 * @date 2018/6/12
 */

@Controller
@RequestMapping("/link/coupQrCode")
public class CoupQrCodeController extends BasicController<EmailInfo> {

    @Resource
    private CouponsService couponsService;

    /**
     * @author wangdawei
     * 请求发送二维码，并保存
     * 优惠卷直接领取页面
     * 保存的时候获取
     * @return
     */
    @RequestMapping("/saveQrCode")
    @ResponseBody
    public Map<String, Object> saveQrCode(@JsonParam Coupons entity) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            CoreUser user = UserUtil.getUser();
            //如果优惠券可直接领取
            String flag = "Y";
            if (flag.equals(entity.getGetDirectly())) {
                //二维码内容
                String strJson = PropertyUtil.getCommonProperty("couponGetDirectlyURL") + "?" + entity.getId();
                //二维码存放位置
                String file = "/web/file//" + user.getCorpid() + "/";
                String realPath = YmlConstants.getPortalShareFileDir() + file;
                String path = entity.getId() + "_" + DateUtil.dateToStr(new Date(), "yyyyMMddHHmmss") + ".png";
                QRCodeUtil.tomakeMode(strJson, realPath, path, 128, 128);
                entity.setCorpid(user.getCorpid());
                entity.setLoginCorpId(user.getLoginCorpId());
                entity.setCodeUrl(strJson);
                entity.setCodeSavPath(file + path);
                couponsService.update(entity);
                result.put("result", file);
            } else {
                result.put("result", "该优惠卷不可直接领取");
            }
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("Type", "error");
            result.put("result", e.getMessage());
        }
        return result;
    }

}
